//
//  CurrencyViewController.m
//  iExpense
//
//  Created by JackyLiang on 09-7-15.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "CurrencyViewController.h"


@implementation CurrencyViewController
@synthesize currencyTable;
@synthesize currencyNames;
@synthesize currencyKeys;


 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
		NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames"
														 ofType:@"plist"];
		NSDictionary *dict = [[NSDictionary alloc] 
							  initWithContentsOfFile:path];
		self.currencyNames = dict;
		[dict release];
		
		NSArray *array = [[currencyNames allKeys] sortedArrayUsingSelector:
						  @selector(compare:)];
		self.currencyKeys = array;
    }
    return self;
}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}

// Call this function whenever the view will appear
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	self.navigationController.toolbarHidden = YES;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.currencyNames = nil;
    self.currencyKeys = nil;
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [currencyKeys count];;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSString *key = [currencyKeys objectAtIndex:section];
    NSArray *nameSection = [currencyNames objectForKey:key];
    return [nameSection count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    
    NSString *key = [currencyKeys objectAtIndex:section];
    NSArray *nameSection = [currencyNames objectForKey:key];
	
	cell.textLabel.text = [nameSection objectAtIndex:row];
	cell.accessoryType = indexPath.row? UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark;
	NSString *iconFileName = [[nameSection objectAtIndex:row] substringToIndex:3];
	NSString *iconFileNameWithExt = [iconFileName stringByAppendingString:@".png"];
	cell.imageView.image = [UIImage imageNamed:iconFileNameWithExt];
	
    return cell;
}

// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController animated:YES];
	// [anotherViewController release];
	[currencyTable deselectRowAtIndexPath:indexPath animated:NO];
	
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [currencyKeys objectAtIndex:section];
    return key;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return currencyKeys;
}


- (void)dealloc {
	[currencyTable release];
	[currencyNames release];
	[currencyKeys release];
    [super dealloc];
}


@end
